'\" t
.\"     Title: LISTEN
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\"      Date: 2011-12-01
.\"    Manual: PostgreSQL 9.1.2 Documentation
.\"    Source: PostgreSQL 9.1.2
.\"  Language: English
.\"
.TH "LISTEN" "7" "2011-12-01" "PostgreSQL 9.1.2" "PostgreSQL 9.1.2 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
LISTEN \- listen for a notification
.\" LISTEN
.SH "SYNOPSIS"
.sp
.nf
LISTEN \fIchannel\fR
.fi
.SH "DESCRIPTION"
.PP

LISTEN
registers the current session as a listener on the notification channel named
\fIchannel\fR\&. If the current session is already registered as a listener for this notification channel, nothing is done\&.
.PP
Whenever the command
NOTIFY \fIchannel\fR
is invoked, either by this session or another one connected to the same database, all the sessions currently listening on that notification channel are notified, and each will in turn notify its connected client application\&.
.PP
A session can be unregistered for a given notification channel with the
UNLISTEN
command\&. A session\*(Aqs listen registrations are automatically cleared when the session ends\&.
.PP
The method a client application must use to detect notification events depends on which
PostgreSQL
application programming interface it uses\&. With the
libpq
library, the application issues
LISTEN
as an ordinary SQL command, and then must periodically call the function
\fBPQnotifies\fR
to find out whether any notification events have been received\&. Other interfaces such as
libpgtcl
provide higher\-level methods for handling notify events; indeed, with
libpgtcl
the application programmer should not even issue
LISTEN
or
UNLISTEN
directly\&. See the documentation for the interface you are using for more details\&.
.PP

\fBNOTIFY\fR(7)
contains a more extensive discussion of the use of
LISTEN
and
NOTIFY\&.
.SH "PARAMETERS"
.PP
\fIchannel\fR
.RS 4
Name of a notification channel (any identifier)\&.
.RE
.SH "NOTES"
.PP

LISTEN
takes effect at transaction commit\&. If
LISTEN
or
UNLISTEN
is executed within a transaction that later rolls back, the set of notification channels being listened to is unchanged\&.
.PP
A transaction that has executed
LISTEN
cannot be prepared for two\-phase commit\&.
.SH "EXAMPLES"
.PP
Configure and execute a listen/notify sequence from
psql:
.sp
.if n \{\
.RS 4
.\}
.nf
LISTEN virtual;
NOTIFY virtual;
Asynchronous notification "virtual" received from server process with PID 8448\&.
.fi
.if n \{\
.RE
.\}
.SH "COMPATIBILITY"
.PP
There is no
LISTEN
statement in the SQL standard\&.
.SH "SEE ALSO"
\fBNOTIFY\fR(7), \fBUNLISTEN\fR(7)
